package com.wufun.overseas.view.ptr;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;

import com.wufun.overseas.R;
import com.wufun.overseas.utils.Dip2SpUtil;
import com.wufun.overseas.utils.PtrLocalDisplay;
import com.wufun.overseas.view.header.DropBounceHeaderView;
import com.wufun.overseas.view.header.MaterialHeader;
import com.wufun.overseas.view.header.RentalsSunHeaderView;
import com.wufun.overseas.view.header.StoreHouseHeader;
;


public class PtrClassicFrameLayout extends PtrFrameLayout {

    enum Style {
        Classical,
        DropBounce,
        Material,
        RentalsSun,
        StoreHouse
    }

    private Style mStyle = Style.DropBounce;

    //region Construct
    public PtrClassicFrameLayout(Context context) {
        super(context);
        initViews();
    }

    public PtrClassicFrameLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        initViews();
    }

    public PtrClassicFrameLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initViews();
    }
    //endregion

    private void initStyle() {
        if (headerStyle == 1) {
            mStyle = Style.Classical;
        } else if (headerStyle == 2) {
            mStyle = Style.DropBounce;
        } else if (headerStyle == 3) {
            mStyle = Style.Material;
        } else if (headerStyle == 4) {
            mStyle = Style.RentalsSun;
        } else if (headerStyle == 5) {
            mStyle = Style.StoreHouse;
        } else {
            mStyle = Style.DropBounce;
        }
    }

    private void initViews() {
        initStyle();

        switch (mStyle) {
            case Classical:
                initClassicViews();
                return;

            case DropBounce:
                initDropBounceHeaderView();
                break;

            case Material:
                initMaterialHeaderView();
                break;

            case RentalsSun:
                initRentalsSunHeaderView();
                break;

            case StoreHouse:
                initStoreHouseHeaderView();
                break;

        }


        this.setHeaderView(mPtrHeader);
        this.addPtrUIHandler(mPtrHeader);
    }

    //region CustomUiHeader
    private PtrUIHeader mPtrHeader;

    private void initStoreHouseHeaderView() {
        StoreHouseHeader header      = new StoreHouseHeader(getContext());
        final String[]   mStringList = {"PAPA LOADING..."};
        header.setPadding(0, PtrLocalDisplay.dp2px(15), 0, 0);
        header.initWithString(mStringList[0]);
        setBackgroundColor(getResources().getColor(R.color.merchant_listing_name_color));

        mPtrHeader = header;
    }

    private void initRentalsSunHeaderView() {
        RentalsSunHeaderView header = new RentalsSunHeaderView(getContext());
        header.setLayoutParams(new PtrFrameLayout.LayoutParams(-1, -2));
        header.setPadding(0, PtrLocalDisplay.dp2px(15), 0, PtrLocalDisplay.dp2px(10));
        header.setUp(this);

        mPtrHeader = header;
    }

    private void initMaterialHeaderView() {
        // header
        MaterialHeader header = new MaterialHeader(getContext());
        int[]          colors = getResources().getIntArray(R.array.materia_colors);
        header.setColorSchemeColors(colors);
        header.setLayoutParams(new PtrFrameLayout.LayoutParams(-1, -2));
        header.setPadding(0, PtrLocalDisplay.dp2px(15), 0, PtrLocalDisplay.dp2px(10));
        header.setPtrFrameLayout(this);

        mPtrHeader = header;
    }

    private void initDropBounceHeaderView() {
        Context ctx    = getContext();
        DropBounceHeaderView header = new DropBounceHeaderView(ctx);
        header.setUp(this);
        header.setColor(Color.parseColor("#4EA9E9"));
        header.setLayoutParams(new PtrFrameLayout.LayoutParams(-1, 50));
        header.setPadding(0, Dip2SpUtil.dip2px(ctx, 8), 0, Dip2SpUtil.dip2px(ctx, 9));

        setLoadingMinTime(1000);
        setDurationToCloseHeader(500);

        mPtrHeader = header;
    }

    public PtrUIHeader getHeader() {
        return mPtrHeader;
    }
    //endregion

    //region Classical UI Header
    private PtrClassicDefaultHeader mPtrClassicHeader;

    public PtrClassicDefaultHeader getClassicHeader() {
        return mPtrClassicHeader;
    }

    private void initClassicViews() {
        mPtrClassicHeader = new PtrClassicDefaultHeader(this.getContext());

        this.setHeaderView(mPtrClassicHeader);
        this.addPtrUIHandler(mPtrClassicHeader);
    }

    /**
     * Specify the last update time by this key string
     *
     * @param key
     */
    public void setLastUpdateTimeKey(String key) {
        if (mPtrClassicHeader != null) {
            mPtrClassicHeader.setLastUpdateTimeKey(key);
        }
    }

    /**
     * Using an object to specify the last update time.
     *
     * @param object
     */
    public void setLastUpdateTimeRelateObject(Object object) {
        if (mPtrClassicHeader != null) {
            mPtrClassicHeader.setLastUpdateTimeRelateObject(object);
        }
    }
    //endregion
}
